********Clean timelines to get precise location, migration, and birth information********

***MXFLS1

*Basic information
cd "$mxfls1rawdata/hh02dta_b3a"
use iiia_portad.dta, clear

*Merge with location information
merge 1:1 folio ls using iiia_mg.dta, nogen keep(match)

gen statebirth=.
replace statebirth=mg01e_2 if mg01e_1==1
replace statebirth=ent if mg01e_1==3

gen state12=.
replace state12=statebirth if mg03==1
replace state12=ent if mg03==3 & mg04e_1==3
replace state12=mg04e_2 if mg03==3 & mg04e_1==1

cd "$mxfls1rawdata/hh02dta_bc"
merge 1:1 folio ls using c_ls, keep(match) nogen

*Generate pid_link in MxFLS1
tostring folio ls, replace
replace ls="0"+ls if length(ls)==1
gen pid_link=folio+ls

destring folio ls pid_link, replace

*Generate birth year
preserve
cd "$mxfls2rawdata/hh05dta_bc"
use c_ls, clear
destring pid_link, replace
keep pid_link ls03_22
duplicates drop pid_link, force
tempfile cls1
save "`cls1'"
restore

merge 1:1 pid_link using "`cls1'", gen(_mergforbirthyr) replace update
drop if _mergforbirthyr==2

gen birthyr=.
replace birthyr=ls03_22 if _mergforbirthyr==4
replace birthyr=2002-ls02_2 if _mergforbirthyr==1

drop _mergforbirthyr

keep folio ls pid_link statebirth state12 birthyr mg03

tempfile mgtemp
save "`mgtemp'", replace

*Locations after that
cd "$mxfls1rawdata/hh02dta_b3a"
use iiia_portad.dta, clear

merge 1:m folio ls using iiia_mg1.dta, nogen keep(match)

gen agemig=mg13_2
gen statemig=.
replace statemig=ent if mg17e_1==3
replace statemig=mg17e_2 if mg17e_1==1
sum secuencia, d
local secmax=r(max)

keep folio ls secuencia agemig statemig

sum secuencia, d
local secmax=r(max)

reshape wide agemig statemig, i(folio ls) j(secuencia)

egen todrop=rownonmiss(agemig*)
drop if todrop==0
drop todrop

egen todrop=rownonmiss(statemig*)
drop if todrop==0
drop todrop

merge 1:1 ls folio using "`mgtemp'", gen(_mergemighist)

forval sec=1/`secmax'{
gen yrmove`sec'=agemig`sec'+birthyr
}

egen yrmoveminmx1=rowmin(yrmove*)

gen yrwhen6=birthyr+6
gen yrwhen12=birthyr+12

sum birthyr, d

local minbirthyr=r(min)


*Moves
forval num=`minbirthyr'/2000 {
gen state`num'=.
*Birth to 6
replace state`num'=statebirth if birthyr<=`num' & yrwhen6>=`num'
*6 to 12
replace state`num'=statebirth if yrwhen6<`num' & yrwhen12>=`num' & mg03==1
replace state`num'=state12 if yrwhen6<`num' & yrwhen12>=`num' & mg03==3
*No moves after 12
replace state`num'=state12 if yrwhen12<`num' & _mergemighist==2
*Moves after 12
replace state`num'=state12 if yrwhen12<`num' & yrmoveminmx1>`num' & _mergemighist==3 &  birthyr<=`num' & yrmoveminmx1!=.
forval sec=1/`secmax' {
replace state`num'=statemig`sec' if yrmove`sec'<=`num' & _mergemighist==3 &  birthyr<=`num' & yrmove`sec'!=. & yrwhen12<`num'
}
}

tempfile migs1
save "`migs1'", replace

***MXFLS2

*Basic information
cd "$mxfls2rawdata/hh05dta_b3a"
use iiia_portad.dta, clear

merge 1:1 folio ls using iiia_mg.dta, nogen keep(match)

*Merge with location information
gen statebirth=.
replace statebirth=mg01e_2 if mg01e_1==1
replace statebirth=ent if mg01e_1==3
gen state12=.
replace state12=statebirth if mg03==1
replace state12=ent if mg03==3 & mg04e_1==3
replace state12=mg04e_2 if mg03==3 & mg04e_1==1

cd "$mxfls2rawdata/hh05dta_bc"
merge 1:1 folio ls using c_ls, keep(match) nogen

*Generate birth year
gen birthyr=ls03_22
keep folio ls pid_link statebirth state12 birthyr mg03

tempfile mgtemp
save "`mgtemp'", replace

*Locations after that
cd "$mxfls2rawdata/hh05dta_b3a"
use iiia_portad.dta, clear

merge 1:m folio ls using iiia_mg1.dta, nogen keep(match)

gen agemig=mg13_2
gen statemig=.
replace statemig=ent if mg17e_1==3
replace statemig=mg17e_2 if mg17e_1==1

sum secuencia, d
local secmax=r(max)

keep folio ls pid_link secuencia agemig statemig

sum secuencia, d
local secmax=r(max)

reshape wide agemig statemig, i(folio ls) j(secuencia)

egen todrop=rownonmiss(agemig*)
drop if todrop==0
drop todrop

egen todrop=rownonmiss(statemig*)
drop if todrop==0
drop todrop

merge 1:1 ls folio using "`mgtemp'", gen(_mergemighist)

forval sec=1/`secmax'{
gen yrmove`sec'=agemig`sec'+birthyr
}

egen yrmoveminmx2=rowmin(yrmove*)

gen yrwhen6=birthyr+6
gen yrwhen12=birthyr+12

destring pid_link, replace
destring ls folio, replace

merge 1:1 pid_link using "`migs1'", gen(_mergemig1_2)


*For non=panel members, get moves as before
preserve
keep if _mergemig1_2==1

capture drop state19?? state20??

sum birthyr, d

*Moves
local minbirthyr=r(min)
forval num=`minbirthyr'/2004 {
gen state`num'=.
*Birth to 6
replace state`num'=statebirth if birthyr<=`num' & yrwhen6>=`num'
*6 to 12
replace state`num'=statebirth if yrwhen6<`num' & yrwhen12>=`num' & mg03==1
replace state`num'=state12 if yrwhen6<`num' & yrwhen12>=`num' & mg03==3
*No moves after 12
replace state`num'=state12 if yrwhen12<`num' & _mergemighist==2
*Moves after 12
replace state`num'=state12 if yrwhen12<`num' & yrmoveminmx2>`num' & _mergemighist==3 &  birthyr<=`num' & yrmoveminmx2!=.
forval sec=1/`secmax' {
replace state`num'=statemig`sec' if yrmove`sec'<=`num' & _mergemighist==3 &  birthyr<=`num' & yrmove`sec'!=. & yrwhen12<`num'
}
}

tempfile mg2_new
save "`mg2_new'", replace
restore


*For panel members, attach new info
preserve

keep if _mergemig1_2==3

forval num=2001/2004 {
gen state`num'=.
replace state`num'=state2000 if yrwhen12<`num' & _mergemighist==2
replace state`num'=state2000 if yrwhen12<`num' & yrmoveminmx2>`num' & _mergemighist==3 &  birthyr<=`num' & yrmoveminmx2!=.
forval sec=1/`secmax' {
replace state`num'=statemig`sec' if yrmove`sec'<=`num' & _mergemighist==3 &  birthyr<=`num' & yrmove`sec'!=. & yrwhen12<`num'
}
}

tempfile mg2_old
save "`mg2_old'"

restore

keep if _mergemig1_2==2

append using "`mg2_new'"
append using "`mg2_old'"

destring folio ls pid_link, replace

tempfile migs2
save "`migs2'", replace


***MXFLS3

*Basic information
cd "$mxfls3rawdata/hh09dta_b3a"
use iiia_portad.dta, clear

merge 1:1 folio ls using iiia_mg.dta, nogen keep(match)

*Merge with location information
gen statebirth=.
replace statebirth=mg01e_2 if mg01e_1==1
replace statebirth=ent if mg01e_1==3

gen state12=.
replace state12=statebirth if mg03==1
replace state12=ent if mg03==3 & mg04e_1==3
replace state12=mg04e_2 if mg03==3 & mg04e_1==1


*Generate birth year
preserve
cd "$mxfls3rawdata/hh09dta_bc"
use c_ls, clear
duplicates drop folio ls, force
tempfile ctemp
save "`ctemp'"
restore

cd "$mxfls3rawdata/hh09dta_bc"
merge 1:1 folio ls using "`ctemp'", keep(match) nogen

gen birthyr=ls03_22

keep folio ls pid_link statebirth state12 birthyr mg03

cd "$mxfls3rawdata/hh09dta_b3a"
tempfile mgtemp
save "`mgtemp'", replace

*Locations after that
cd "$mxfls3rawdata/hh09dta_b3a"
use iiia_portad.dta, clear

merge 1:m folio ls using iiia_mg1.dta, nogen keep(match)

gen agemig=mg13_2

gen statemig=.
replace statemig=ent if mg17e_1==3
replace statemig=mg17e_2 if mg17e_1==1

sum secuencia, d
local secmax=r(max)

keep folio ls secuencia agemig statemig

sum secuencia, d
local secmax=r(max)

reshape wide agemig statemig, i(folio ls) j(secuencia)

egen todrop=rownonmiss(agemig*)
drop if todrop==0
drop todrop

egen todrop=rownonmiss(statemig*)
drop if todrop==0
drop todrop

merge 1:1 ls folio using "`mgtemp'", gen(_mergemighist)

forval sec=1/`secmax'{
gen yrmove`sec'=agemig`sec'+birthyr
}

egen yrmoveminmx3=rowmin(yrmove*)

gen yrwhen6=birthyr+6
gen yrwhen12=birthyr+12


*Merge with before
local lets P H 
local i=0
foreach let in `lets' {
local i=`i'+1
if `i'==1{
gen posi=strpos(folio,"`let'")
gen folio1=substr(folio,1,posi-2)
gen folio2=substr(folio,posi+1,length(folio))
gen folioint=folio1+folio2

gen posi1=strpos(pid_link,"`let'")
gen pidi1=substr(pid_link,1,posi1-2)
gen pidi2=substr(pid_link,posi1+1,length(pid_link))
gen pidiint=pidi1+pidi2
}
if `i'==2{
gen posi2=strpos(folio,"`let'")
gen folio12=substr(folio,1,posi2-2)
gen folio22=substr(folio,posi2+1,length(folio))
gen folioint2=folio12+folio22

gen posi12=strpos(pid_link,"`let'")
gen pidi12=substr(pid_link,1,posi12-2)
gen pidi22=substr(pid_link,posi12+1,length(pid_link))
gen pidiint2=pidi12+pidi22
}
}

rename folio folioold
gen folio=folioint if posi2==0
replace folio=folioint2 if posi2!=0

rename pid_link pidlinkold
gen pid_link=pidiint if posi12==0
replace pid_link=pidiint2 if posi12!=0

duplicates drop pid_link, force

destring folio ls pid_link, replace

merge 1:1 pid_link using "`migs2'", gen(_mergemig2_3)


*For non=panel members, get moves as before
preserve

keep if _mergemig2_3==1

capture drop state19?? state20??

sum birthyr, d

local minbirthyr=r(min)
forval num=`minbirthyr'/2014 {
gen state`num'=.
*Birth to 6
replace state`num'=statebirth if birthyr<=`num' & yrwhen6>=`num'
*6 to 12
replace state`num'=statebirth if yrwhen6<`num' & yrwhen12>=`num' & mg03==1
replace state`num'=state12 if yrwhen6<`num' & yrwhen12>=`num' & mg03==3
*No moves after 12
replace state`num'=state12 if yrwhen12<`num' & _mergemighist==2
*Moves after 12
replace state`num'=state12 if yrwhen12<`num' & yrmoveminmx3>`num' & _mergemighist==3 &  birthyr<=`num' & yrmoveminmx3!=.
forval sec=1/`secmax' {
replace state`num'=statemig`sec' if yrmove`sec'<=`num' & _mergemighist==3 &  birthyr<=`num' & yrmove`sec'!=. & yrwhen12<`num'
}
}

tempfile mg3_new
save "`mg3_new'", replace
restore


*For panel members, attach new info
preserve
keep if _mergemig2_3==3

forval num=2005/2014 {
gen state`num'=.
replace state`num'=state2004 if yrwhen12<`num' & _mergemighist==2
replace state`num'=state2004 if yrwhen12<`num' & yrmoveminmx3>`num' & _mergemighist==3 &  birthyr<=`num' & yrmoveminmx3!=.
forval sec=1/`secmax' {
replace state`num'=statemig`sec' if yrmove`sec'<=`num' & _mergemighist==3 &  birthyr<=`num' & yrmove`sec'!=. & yrwhen12<`num'
}
}

tempfile mg3_old
save "`mg3_old'"

restore

keep if _mergemig2_3==2

append using "`mg3_new'"
append using "`mg3_old'"


tempfile migs3
save "`migs3'", replace

*Generate migration variables

forval mx=2/3 {
capture drop hol2 dif*
gen migrate`mx' = .
la var migrate`mx' "Migrate across province lines before mxfls`mx'"

gen migrate_num`mx' = .
la var migrate_num`mx' "# of times migrate across province lines before mxfls`mx'"

if `mx'==2 {
local yrmax 2006
}
if `mx'==3{
local yrmax 2014
}

forval yr=1907/`yrmax' {
local yrmin1=`yr'-1
gen  dif`yr'=.
replace dif`yr'=0 if state`yr'==state`yrmin1' & state`yr'!=. & state`yrmin1'!=.
replace dif`yr'=1 if state`yr'!=state`yrmin1' & state`yr'!=. & state`yrmin1'!=.

}

egen hol2=rowtotal(dif*)

replace migrate_num`mx'=hol2 if hol2!=.

replace migrate`mx'=0 if hol2==0 & hol2!=.
replace migrate`mx'=1 if hol2!=0 & hol2!=.

}

egen earliestyrmig2=rowmin(yrmoveminmx1 yrmoveminmx2)
gen earliestyrmig3=yrmoveminmx3
keep pid_link migrate? migrate_num? state19?? state20?? statebirth birthyr earliestyrmig?
reshape long migrate migrate_num earliestyrmig, i(pid_link) j(mxfls)





